package cn.com.dcsgo.service.impl;

import cn.com.dcsgo.domain.bo.CustomerTotalHistoricalTransactionAmountBO;
import cn.com.dcsgo.domain.dto.CustomerPageDTO;
import cn.com.dcsgo.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.dcsgo.domain.po.Customer;
import cn.com.dcsgo.service.CustomerService;
import cn.com.dcsgo.mapper.CustomerMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 针对表【customer(客户表)】的数据库操作Service实现
 *
 * @author Dcsgo
 * @since 13/5/2025 上午 10:54
 */
@Service
@RequiredArgsConstructor
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
        implements CustomerService {
    private final OrderService orderService;


    @Transactional(rollbackFor = {Throwable.class})
    @Override
    public IPage<Customer> getPage(Page<Customer> pageParam, CustomerPageDTO customerPageDTO) {
        // 更新客户历史交易总额
        this.update(orderService.getCustomerTotalHistoricalTransactionAmountBOList());

        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        // 封装查询条件
        if (customerPageDTO.getId() != null) {
            queryWrapper.eq(Customer::getId, customerPageDTO.getId());
        }
        if (StringUtils.hasText(customerPageDTO.getCustName())) {
            queryWrapper.like(Customer::getCustName, customerPageDTO.getCustName());
        }
        if (StringUtils.hasText(customerPageDTO.getPhone())) {
            queryWrapper.like(Customer::getPhone, customerPageDTO.getPhone());
        }
        if (StringUtils.hasText(customerPageDTO.getAddress())) {
            queryWrapper.like(Customer::getAddress, customerPageDTO.getAddress());
        }
        if (customerPageDTO.getGrade() != null) {
            queryWrapper.eq(Customer::getGrade, customerPageDTO.getGrade());
        }
        if (StringUtils.hasText(customerPageDTO.getCustType())) {
            queryWrapper.like(Customer::getCustType, customerPageDTO.getCustType());
        }
        if (customerPageDTO.getTotalHistoricalTransactionAmount() != null
                && customerPageDTO.getTotalHistoricalTransactionAmount() > 0) {
            queryWrapper.ge(Customer::getTotalHistoricalTransactionAmount,
                    customerPageDTO.getTotalHistoricalTransactionAmount());
        }
        // 根据历史交易总额降序排序
        queryWrapper.orderByDesc(Customer::getTotalHistoricalTransactionAmount);
        return page(pageParam, queryWrapper);
    }

    @Transactional(rollbackFor = {Throwable.class})
    @Override
    public void update(List<CustomerTotalHistoricalTransactionAmountBO> customerTotalHistoricalTransactionAmountBOList) {
        // 所有客户历史交易总额信息为空 提前退出
        if (CollectionUtils.isEmpty(customerTotalHistoricalTransactionAmountBOList)) {
            return;
        }
        List<Long> custIdList = customerTotalHistoricalTransactionAmountBOList.stream().map(
                CustomerTotalHistoricalTransactionAmountBO::getCustId
        ).toList();
        // 获取有历史交易记录的客户信息
        List<Customer> customerList = list(new LambdaQueryWrapper<Customer>().in(Customer::getId, custIdList));
        // 设置各个客户的历史交易总额
        customerTotalHistoricalTransactionAmountBOList.forEach(
                bo -> customerList.forEach(customer -> {
                    if (customer.getId().equals(bo.getCustId())) {
                        customer.setTotalHistoricalTransactionAmount(bo.getTotalHistoricalTransactionAmount());
                    }
                })
        );
        updateBatchById(customerList);
    }
}




